 /*
 * @description: 新人专享
 * @Author: along
 * @Date: 2021-06-15
 * @Last Modified by: along
 * @Last Modified time: 2021-06-15
 */
<template>
    <div
        class="newPeople"
        @click="emitEvent(7)"
        v-if="isLoad"
    >
        <div
            class="__wrap"
            :style="{
                background: 'linear-gradient(135deg, #FF5064 0%, #FC334A 100%)',
                borderRadius: '8px'
            }"
        >
            <div class="__header">
                <img
                    src="https://cdn.zsdx.cn/student-app/images/active-home/people_logo.png"
                    class="__header_logo"
                >
                <div class="__view">
                    <img
                        v-for="(el,index) in welfare_info.view_list.slice(0,3)"
                        :key="index"
                        :src="el.head_img"
                        class="__images"
                        :style="{
                            left: index === 1 ? '-8px' : index === 2 ? '-16px' : '0px'
                        }"
                    >
                    <p class="__text">
                        {{ welfare_info.sale_count }}新人已下单
                    </p>
                </div>
            </div>
            <div class="__box">
                <div class="__nav">
                    <p class="__nav_title">
                        新人特价 下单送券
                    </p>
                    <div class="__date">
                        <div
                            v-if="!!(~~formData.day)"
                            class="__day"
                        >
                            {{ formData.day }}
                        </div>
                        <div
                            v-if="!!(~~formData.day)"
                            class="__day_text"
                        >
                            天
                        </div>
                        <div class="__day">
                            {{ formData.hour }}
                        </div>
                        <div class="__side">
                            :
                        </div>
                        <div class="__day">
                            {{ formData.minutes }}
                        </div>
                        <div class="__side">
                            :
                        </div>
                        <div class="__day">
                            {{ formData.seconds }}
                        </div>
                    </div>
                </div>
                <div class="__shop">
                    <div
                        v-for="(row,index) in welfare_info.top_list"
                        :key="index"
                        class="__items"
                    >
                        <img
                            :src="row.goods_cover"
                            class="__items_cover"
                        >
                        <p class="__items_title">
                            {{ row.goods_title }}
                        </p>
                        <div class="__items_info">
                            <p class="__items_price">
                                ¥{{ row.goods_price }}
                            </p>
                            <p class="__items_origin_price">
                                ¥{{ row.goods_price_origin }}
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'NewPeople',
    props: {
        // baseconfig: {
        //     type: Object,
        //     default: () => {}
        // },
        index: {//存在多个相同组件,查询当前位置
            type: Number,
            default: () => {}
        }
    },
    data () {
        return {
            // rate: '0px 0px 0px 0px',
            isLoad: false,
            welfare_info: {
                "now_time": 0,
                "end_time": 0,
                "sale_count": 0,
                "view_count": 0,
                "view_list": [],
                "top_list": []
            },
            formData: {
                day: '',
                hour: '',
                minutes: '',
                seconds: ''
            }
        };
    },
    watch: {
        //圆角单位转换
        // baseconfig:  {
        //     handler: function (val, oldVal) {
        //         let rateValue = val.style.borderRadius;

        //         this.rate = rateValue.replace(/rpx/g, 'px');
        //         this.isLoad = true;
        //     },
        //     deep: true
        // }
    },
    mounted () {
        this.isLoad = true;
        // if(JSON.stringify(this.baseconfig.style) !== '{}') {//回填圆角
        //     let rateValue = this.baseconfig.style.borderRadius;

        //     this.rate = rateValue.replace(/rpx/g, 'px');
        // }

        this.$post('/student/custom_page_for_shop_index%5Cget_data_for_app_show', {
            item_type: 7,
            data_json: JSON.stringify({})
        }, resp => {
            if(resp.code == 1){
                this.welfare_info = {
                    ...resp.data.data
                };

                this.countDown(this.welfare_info.now_time * 1000, this.welfare_info.end_time * 1000);
            }
        });
    },
    methods: {
        /**
         * @description 点击组件，向上级传递点击类型
         */
        emitEvent (type) {
            this.$emit('emit', {
                type: type,//组件类型
                index: this.index//组件位置
            });
        },

        /**
         * @deprecated 处理倒计时
         * @param {Number}  start_time  //开始时间
         * @param {Number}  end_time    //结束时间
         * @param {Number}  type        //类型 0:已结束 1:进行中 2:未开始
         *  desc: 只要不是已结束 一直递归下去更新时间
         */
        countDown (start_time, end_time) {
            const now_date = new Date().getTime();
            const now_type = Number(end_time) < Number(now_date) ? 0 : (Number(start_time) > Number(now_date) ? 2 : 1);

            if(now_type === 0) {
                clearTimeout(this.count_timeout);
                return false;
            }

            const wait_time = now_type == 1 ? (end_time - now_date) : (start_time - now_date);
            const days = parseInt(wait_time / (1000 * 60 * 60 * 24));
            const hours = parseInt((wait_time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            const minutes = parseInt((wait_time % (1000 * 60 * 60)) / (1000 * 60));
            const seconds = ((wait_time % (1000 * 60)) / 1000).toFixed(0);
            const repair = data => {
                if(data < 10) return '0' + data;
                return data;
            };

            this.formData = {
                day: repair(days),
                hour: repair(hours),
                minutes: repair(minutes),
                seconds: repair(seconds)
            };
            this.count_timeout = setTimeout(() => {
                this.countDown(start_time, end_time);
            }, 1000);
        },
    }
};
</script>

<style lang="less" scoped>
.newPeople {
    width: 375px;
    box-sizing: border-box;
    border: 1px #ffffff solid;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: rgba(242, 242, 242, 1);
    .__wrap {
        width: 355px;
        height: 271px;
        box-sizing: border-box;
        padding: 12px 0 10px 0;
        .__header {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            .__header_logo {
                width: 177px;
                height: 28px;
                margin-left: 6px;
            }
            .__view {
                height: 28px;
                background: rgba(255, 255, 255, 0.2);
                border-radius: 100px 0px 0px 100px;
                display: flex;
                align-items: center;
                box-sizing: border-box;
                padding: 0 8px 0 3px;
                .__images {
                    width: 22px;
                    height: 22px;
                    border-radius: 50%;
                    border: 2px solid #f2f2f2;
                    position: relative;
                }
                .__text {
                    color: rgba(255, 255, 255, 1);
                    font-size: 10px;
                    font-weight: 500;
                    white-space: nowrap;
                    margin-left: -12px;
                }
            }
        }
        .__box {
            width: 335px;
            height: 209px;
            background: rgba(251,237,278,1);
            border-radius: 8px;
            margin-top: 12px;
            margin-left: 10px;
            .__nav {
                width: 100%;
                height: 42px;
                display: flex;
                box-sizing: border-box;
                padding: 12px 10px;
                align-items: center;
                justify-content: space-between;
                .__nav_title {
                    color: rgba(93, 2, 1, 1);
                    font-size: 14px;
                    font-weight: 500;
                }
                .__date {
                    height: 18px;
                    display: flex;
                    align-items: center;
                    .__day {
                        width: 18px;
                        height: 18px;
                        background: #5D0201;
                        border-radius: 2px;
                        text-align: center;
                        line-height: 18px;
                        color: #ffffff;
                        font-size: 12px;
                        font-weight: 500;
                    }
                    .__day_text {
                        color: rgba(93, 2, 1, 1);
                        font-size: 10px;
                        margin: 0 3px;
                    }
                    .__side {
                        color: rgba(93, 2, 1, 1);
                        font-size: 14px;
                        font-weight: 500;
                        margin: 0 3px;
                    }
                }
            }
            .__shop {
                width: 335px;
                height: 167px;
                background: #ffffff;
                border-radius: 8px;
                box-sizing: border-box;
                padding: 10px 10px 12px 10px;
                display: flex;
                align-items: center;
                justify-content: space-between;
                .__items {
                    width: 101px;
                    height: 145px;
                    display: flex;
                    flex-direction: column;
                    .__items_cover {
                        width: 101px;
                        height: 101px;
                        border-radius: 6px;
                        margin-bottom: 8px;
                    }
                    .__items_title {
                        width: 101px;
                        height: 14px;
                        color: rgba(104, 93, 93, 1);
                        font-size: 12px;
                        line-height: 14px;
                        overflow: hidden;
                        white-space: nowrap;
                        text-overflow: ellipsis;
                        margin-bottom: 8px;
                    }
                    .__items_info {
                        width: 101px;
                        height: 14px;
                        display: flex;
                        .__items_price {
                            height: 14px;
                            color: rgba(249, 91, 86, 1);
                            line-height: 14px;
                            font-weight: 500;
                            margin-right: 4px;
                        }
                        .__items_origin_price {
                            color: rgba(160, 153, 153, 1);
                            font-size: 12px;
                            height: 12px;
                            line-height: 12px;
                            margin-top: 2px;
                        }
                    }
                }
            }
        }
    }
}
</style>